﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GatewayInspect.Common;
using System.Data.OracleClient;
using System.Media;

namespace GatewayInspect.Modules
{
    public partial class FczlDJKModule : BaseModule
    {
        public FczlDJKModule()
        {
            InitializeComponent();
        }
        protected override void OnInitialize()
        {
            base.OnInitialize();
            foreach (ToolStripItem item in this.tsMain.Items)
            {
                if (item != tsBtnRefresh && item != tsBtnClose && item != toolStripSeparator3)
                {
                    item.Visible = false;
                }
            }
        }
        string NUM = "0";
        string NUM1 = "0";
        
        protected override void OnLoadModule()
        {
            base.OnLoadModule();
            if (Session.CurrentUserName == "ADMIN_DJK" || Session.CurrentUserName == "ADMIN")
            {
                //实华董家口
                string strSql = "select t.PLAN_NO,t.TRUCK_NO,ORDER_TIME,decode(t.IF_END, '0','在场','1','离场') IF_END,decode(t.ORGCODE, '01','实华黄岛','02','实华董家口','03','摩科瑞','21','红星库区','22','天安','23','丽星','24','益佳','25','中油','11','平安停车场1','12','平安停车场2','13','平安停车场3','15','安顺停车场1','16','安顺停车场2','17','董家口停车场','18','摩科瑞停车场','23','金赛物流停车场') ORGNAME,t.IN_PARK_TIME AS IN_DATE from TRUCK_IN_ORDER_TNO t, wlw_dbc_oil_plan p where t.IF_END='0' and t.plan_no=p.Plan_No and p.depot_code='02'  ORDER BY t.ORDER_TIME ASC";
                DataTable dtCYJL = DataAccess.ExecuteDataTable(strSql);
                if (dtCYJL != null && dtCYJL.Rows.Count > 0)
                {
                    dtCYJL.Columns.Add("DRIVER_TEL", typeof(string));
                    for (int i = 0; i < dtCYJL.Rows.Count; i++)
                    {
                        string TRUCK_NO = dtCYJL.Rows[i]["TRUCK_NO"].ToString();
                        string PLAN_NO = dtCYJL.Rows[i]["PLAN_NO"].ToString();
                        string strSqlTel = "select d.phone as DRIVER_TEL from wlw_dbc_driver d where d.id in (select b.driver_id from wlw_dbc_oil_bill b where b.plan_no= '" + PLAN_NO + "' and head_car_id in (select i.id from wlw_dbc_oilcar_info i where i.car_no= '" + TRUCK_NO + "'))";
                        DataTable dtTel = DataAccess.ExecuteDataTable(strSqlTel);
                        if (dtTel != null && dtTel.Rows.Count > 0)
                        {
                            string DRIVER_TEL = dtTel.Rows[0]["DRIVER_TEL"].ToString();
                            dtCYJL.Rows[i]["DRIVER_TEL"] = DRIVER_TEL;
                        }
                    }
                    
                }
                this.cdgvGZJL.AutoGenerateColumns = false;
                this.cdgvGZJL.DataSource = dtCYJL;

                //进行统计
                string strSqlNum = "select count(*) as NUM from TRUCK_IN_ORDER_TNO t, wlw_dbc_oil_plan p where t.IF_END='0' and t.plan_no=p.Plan_No and p.depot_code='02'  ORDER BY t.ORDER_TIME ASC";
                DataTable dtCYJLNum = DataAccess.ExecuteDataTable(strSqlNum);
                string num = dtCYJLNum.Rows[0]["NUM"].ToString();
                this.label2.Text = num;
                if(num!="0")
                {
                    //董家口停车场
                    string strSqlPA1 = "select count(*) as NUM from TRUCK_IN_ORDER_TNO t, wlw_dbc_oil_plan p where t.ORGCODE='17' AND  t.IF_END='0' and t.plan_no=p.Plan_No and p.depot_code='02'  ORDER BY t.ORDER_TIME ASC";
                    DataTable dtCYJLPA1 = DataAccess.ExecuteDataTable(strSqlPA1);
                    string numPA1 = dtCYJLPA1.Rows[0]["NUM"].ToString();

                    //摩科瑞停车场
                    string strSqlPA2 = "select count(*) as NUM from TRUCK_IN_ORDER_TNO t, wlw_dbc_oil_plan p where t.ORGCODE='18' AND  t.IF_END='0' and t.plan_no=p.Plan_No and p.depot_code='02'  ORDER BY t.ORDER_TIME ASC";
                    DataTable dtCYJLPA2 = DataAccess.ExecuteDataTable(strSqlPA2);
                    string numPA2 = dtCYJLPA2.Rows[0]["NUM"].ToString();
                    this.label19.Text = "董家口停车场：" + numPA1 + "   摩科瑞停车场：" + numPA2 ;
                } if (num == "0") {
                    this.label19.Text = "";
                }
       



                //摩科瑞
                string strSql1 = "select t.PLAN_NO,t.TRUCK_NO,ORDER_TIME,decode(t.IF_END, '0','在场','1','离场') IF_END,decode(t.ORGCODE, '01','实华黄岛','02','实华董家口','03','摩科瑞','21','红星库区','22','天安','23','丽星','24','益佳','25','中油','11','平安停车场1','12','平安停车场2','13','平安停车场3','15','安顺停车场1','16','安顺停车场2','17','董家口停车场','18','摩科瑞停车场','23','金赛物流停车场') ORGNAME,t.IN_PARK_TIME AS IN_DATE from TRUCK_IN_ORDER_TNO t, wlw_dbc_oil_plan p where  t.IF_END='0' and t.plan_no=p.Plan_No and p.depot_code='03'  ORDER BY t.ORDER_TIME ASC";
                DataTable dtCYJL1 = DataAccess.ExecuteDataTable(strSql1);
               
                if (dtCYJL1 != null && dtCYJL1.Rows.Count > 0)
                {
                    dtCYJL1.Columns.Add("DRIVER_TEL", typeof(string));
                    for (int i = 0; i < dtCYJL1.Rows.Count; i++)
                    {
                        string TRUCK_NO = dtCYJL1.Rows[i]["TRUCK_NO"].ToString();
                        string PLAN_NO = dtCYJL1.Rows[i]["PLAN_NO"].ToString();
                        string strSqlTel = "select d.phone as DRIVER_TEL from wlw_dbc_driver d where d.id in (select b.driver_id from wlw_dbc_oil_bill b where b.plan_no= '" + PLAN_NO + "' and head_car_id in (select i.id from wlw_dbc_oilcar_info i where i.car_no= '" + TRUCK_NO + "'))";
                        DataTable dtTel = DataAccess.ExecuteDataTable(strSqlTel);
                        if (dtTel != null && dtTel.Rows.Count > 0)
                        {
                            string DRIVER_TEL = dtTel.Rows[0]["DRIVER_TEL"].ToString();
                            dtCYJL1.Rows[i]["DRIVER_TEL"] = DRIVER_TEL;
                        }
                    }
                   
                }
                this.cDataGridView1.AutoGenerateColumns = false;
                this.cDataGridView1.DataSource = dtCYJL1;
                //进行统计
                string strSql1Num = "select count(*) as NUM from TRUCK_IN_ORDER_TNO t, wlw_dbc_oil_plan p where t.IF_END='0' and t.plan_no=p.Plan_No and p.depot_code='03'  ORDER BY t.ORDER_TIME ASC";
                DataTable dtCYJL1Num = DataAccess.ExecuteDataTable(strSql1Num);
                string num1 = dtCYJL1Num.Rows[0]["NUM"].ToString();
                this.label10.Text = num1;

                if (num1 != "0")
                {
                    //停车场1车数
                    string strSqlPA1 = "select count(*) as NUM from TRUCK_IN_ORDER_TNO t, wlw_dbc_oil_plan p where t.ORGCODE='17' AND  t.IF_END='0' and t.plan_no=p.Plan_No and p.depot_code='03'  ORDER BY t.ORDER_TIME ASC";
                    DataTable dtCYJLPA1 = DataAccess.ExecuteDataTable(strSqlPA1);
                    string numPA1 = dtCYJLPA1.Rows[0]["NUM"].ToString();

                    //停车场2车数
                    string strSqlPA2 = "select count(*) as NUM from TRUCK_IN_ORDER_TNO t, wlw_dbc_oil_plan p where t.ORGCODE='18' AND  t.IF_END='0' and t.plan_no=p.Plan_No and p.depot_code='03'  ORDER BY t.ORDER_TIME ASC";
                    DataTable dtCYJLPA2 = DataAccess.ExecuteDataTable(strSqlPA2);
                    string numPA2 = dtCYJLPA2.Rows[0]["NUM"].ToString();


                    this.label20.Text = "董家口停车场：" + numPA1 + " 摩科瑞停车场：" + numPA2;
                } if (num1 == "0")
                {
                    this.label20.Text = "";
                }
                int num_int = 0;
                int num1_int = 0;
                int NUM_INT = 0;
                int NUM1_INT = 0;
                try
                {
                    num_int = int.Parse(num);
                    num1_int = int.Parse(num1);     
                    NUM_INT = int.Parse(NUM);
                    NUM1_INT = int.Parse(NUM1);
                  
                }
                catch (Exception e)
                {
                    throw e;
                }
                if (num_int > NUM_INT || num1_int > NUM1_INT )
                {
                    NUM = num;
                    NUM1 = num1;
                    
                    SoundPlayer p = new SoundPlayer();
                    p.SoundLocation = Application.StartupPath + "\\test.wav";
                    p.Load();
                    p.Play();
                }
                else {
                    NUM = num;
                    NUM1 = num1;
                }
            }
            else if (Session.CurrentOrgType == 0 || Session.CurrentOrgType == 1)
            {

                //实华董家口
                string strSql = "select t.PLAN_NO,t.TRUCK_NO,ORDER_TIME,decode(t.IF_END, '0','在场','1','离场') IF_END,decode(t.ORGCODE, '01','实华黄岛','02','实华董家口','03','摩科瑞','21','红星库区','22','天安','23','丽星','24','益佳','25','中油','11','平安停车场1','12','平安停车场2','13','平安停车场3','15','安顺停车场1','16','安顺停车场2','17','董家口停车场','18','摩科瑞停车场','23','金赛物流停车场') ORGNAME ,t.IN_PARK_TIME AS IN_DATE  from TRUCK_IN_ORDER_TNO t, wlw_dbc_oil_plan p  where    t.IF_END='0' and  t.ORGCODE = '" + Session.CurrentOrgCode + "' and t.plan_no=p.Plan_No and p.depot_code='02'   ORDER BY t.ORDER_TIME ASC";
                DataTable dtCYJL = DataAccess.ExecuteDataTable(strSql);
                if (dtCYJL != null && dtCYJL.Rows.Count > 0)
                {
                    dtCYJL.Columns.Add("DRIVER_TEL", typeof(string));
                    for (int i = 0; i < dtCYJL.Rows.Count; i++)
                    {
                        string TRUCK_NO = dtCYJL.Rows[i]["TRUCK_NO"].ToString();
                        string PLAN_NO = dtCYJL.Rows[i]["PLAN_NO"].ToString();
                        string strSqlTel = "select d.phone as DRIVER_TEL from wlw_dbc_driver d where d.id in (select b.driver_id from wlw_dbc_oil_bill b where b.plan_no= '" + PLAN_NO + "' and head_car_id in (select i.id from wlw_dbc_oilcar_info i where i.car_no= '" + TRUCK_NO + "'))";
                        DataTable dtTel = DataAccess.ExecuteDataTable(strSqlTel);
                        if (dtTel != null && dtTel.Rows.Count > 0)
                        {
                            string DRIVER_TEL = dtTel.Rows[0]["DRIVER_TEL"].ToString();
                            dtCYJL.Rows[i]["DRIVER_TEL"] = DRIVER_TEL;
                        }
                    }
                   
                }
                this.cdgvGZJL.AutoGenerateColumns = false;
                this.cdgvGZJL.DataSource = dtCYJL;
                //进行统计
                string strSqlNum = "select count(*) as NUM from TRUCK_IN_ORDER_TNO t, wlw_dbc_oil_plan p where t.IF_END='0' and  t.ORGCODE = '" + Session.CurrentOrgCode + "' and t.plan_no=p.Plan_No and p.depot_code='02'  ORDER BY t.ORDER_TIME ASC";
                DataTable dtCYJLNum = DataAccess.ExecuteDataTable(strSqlNum);
                string num = dtCYJLNum.Rows[0]["NUM"].ToString();
                this.label2.Text = num;
                
                
                
                //摩科瑞
                string strSql1 = "select t.PLAN_NO,t.TRUCK_NO,ORDER_TIME,decode(t.IF_END, '0','在场','1','离场') IF_END,decode(t.ORGCODE, '01','实华黄岛','02','实华董家口','03','摩科瑞','21','红星库区','22','天安','23','丽星','24','益佳','25','中油','11','平安停车场1','12','平安停车场2','13','平安停车场3','15','安顺停车场1','16','安顺停车场2','17','董家口停车场','18','摩科瑞停车场','23','金赛物流停车场') ORGNAME,t.IN_PARK_TIME AS IN_DATE  from TRUCK_IN_ORDER_TNO t, wlw_dbc_oil_plan p  where    t.IF_END='0' and  t.ORGCODE = '" + Session.CurrentOrgCode + "' and t.plan_no=p.Plan_No and p.depot_code='03'  ORDER BY t.ORDER_TIME ASC";
                DataTable dtCYJL1 = DataAccess.ExecuteDataTable(strSql1);
                if (dtCYJL1 != null && dtCYJL1.Rows.Count > 0)
                {
                    dtCYJL1.Columns.Add("DRIVER_TEL", typeof(string));
                    for (int i = 0; i < dtCYJL1.Rows.Count; i++)
                    {
                        string TRUCK_NO = dtCYJL1.Rows[i]["TRUCK_NO"].ToString();
                        string PLAN_NO = dtCYJL1.Rows[i]["PLAN_NO"].ToString();
                        string strSqlTel = "select d.phone as DRIVER_TEL from wlw_dbc_driver d where d.id in (select b.driver_id from wlw_dbc_oil_bill b where b.plan_no= '" + PLAN_NO + "' and head_car_id in (select i.id from wlw_dbc_oilcar_info i where i.car_no= '" + TRUCK_NO + "'))";
                        DataTable dtTel = DataAccess.ExecuteDataTable(strSqlTel);
                        if (dtTel != null && dtTel.Rows.Count > 0)
                        {
                            string DRIVER_TEL = dtTel.Rows[0]["DRIVER_TEL"].ToString();
                            dtCYJL1.Rows[i]["DRIVER_TEL"] = DRIVER_TEL;
                        }
                    }
                   
                }
                this.cDataGridView1.AutoGenerateColumns = false;
                this.cDataGridView1.DataSource = dtCYJL1;
                //进行统计
                string strSql1Num = "select count(*) as NUM from TRUCK_IN_ORDER_TNO t, wlw_dbc_oil_plan p where t.IF_END='0' and t.ORGCODE = '" + Session.CurrentOrgCode + "' and t.plan_no=p.Plan_No and p.depot_code='03'  ORDER BY t.ORDER_TIME ASC";
                DataTable dtCYJL1Num = DataAccess.ExecuteDataTable(strSql1Num);
                string num1 = dtCYJL1Num.Rows[0]["NUM"].ToString();
                this.label10.Text = num1;

                int num_int = 0;
                int num1_int = 0;
                

                int NUM_INT = 0;
                int NUM1_INT = 0;
                
                try
                {
                    num_int = int.Parse(num);
                    num1_int = int.Parse(num1);
                   

                    NUM_INT = int.Parse(NUM);
                    NUM1_INT = int.Parse(NUM1);
                   
                }
                catch (Exception e)
                {
                    throw e;
                }
                if (num_int > NUM_INT || num1_int > NUM1_INT)
                {
                    NUM = num;
                    NUM1 = num1;
                   
                    SoundPlayer p = new SoundPlayer();
                    p.SoundLocation = Application.StartupPath + "\\test.wav";
                    p.Load();
                    p.Play();
                }
                else
                {
                    NUM = num;
                    NUM1 = num1;
                }

            }
        }

        private void FczlModule_Load(object sender, EventArgs e)
        {
            Control.CheckForIllegalCrossThreadCalls = false;
            timer1.Enabled = true;
            //System.Timers.Timer t = new System.Timers.Timer(2000);   //实例化Timer类，设置间隔时间为10000毫秒；   
            //t.Elapsed += new System.Timers.ElapsedEventHandler(theout); //到达时间的时候执行事件；   
            //t.AutoReset = true;   //设置是执行一次（false）还是一直执行(true)；   
            //t.Enabled = true;     //是否执行System.Timers.Timer.Elapsed事件；   
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            OnLoadModule();
        }
        //public void theout(object source, System.Timers.ElapsedEventArgs e)
        //{
        //    OnLoadModule();
        //}  
    }
}
